package com.water.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 用于ajax返回，转换成json。
 * 
 * @author lihua sun
 *
 */
public class WebResult {

	/**
	 * 执行结果，0 业务异常 -1 系统异常 1 正确执行 2 消息提示
	 */
	private int code;
	private String message;
	private Map<String, Object> data;

	private int recordsTotal;
	private int recordsFiltered;
	private int totalPage;
	private int recordStart;
	private int pageSize;
	private int draw;

	public void add(String name, Object obj) {
		if (data == null) {
			data = new HashMap<String, Object>();
		}
		data.put(name, obj);
	}

	public Object get(String name) {
		Object value = null;
		if (data != null) {
			value = data.get(name);
		}
		return value;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static WebResult success() {
		WebResult w = new WebResult();
		w.setCode(1);
		return w;
	}

	public static WebResult success(String message) {
		WebResult w = new WebResult();
		w.setCode(2);
		w.setMessage(message);
		return w;
	}

	public static WebResult fail(String message) {
		WebResult w = new WebResult();
		w.setCode(0);
		w.setMessage(message);
		return w;
	}

	public static WebResult error(String message) {
		WebResult w = new WebResult();
		w.setCode(-1);
		w.setMessage(message);
		return w;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getRecordsTotal() {
		return recordsTotal;
	}

	public void setRecordsTotal(int recordsTotal) {
		this.recordsTotal = recordsTotal;
	}

	public int getRecordsFiltered() {
		return recordsFiltered;
	}

	public void setRecordsFiltered(int recordsFiltered) {
		this.recordsFiltered = recordsFiltered;
	}

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getRecordStart() {
		return recordStart;
	}

	public void setRecordStart(int recordStart) {
		this.recordStart = recordStart;
	}

	public String toJson() {
		ObjectMapper mapper = new ObjectMapper();
		String st;
		try {
			st = mapper.writeValueAsString(this);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
		return st;
	}

	public static WebResult parse(String str) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		// 解析器支持解析单引号
		mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
		// 解析器支持解析结束符
		mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		WebResult r = mapper.readValue(str, WebResult.class);
		return r;

	}

}
